package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.service.TeacherTaskWorkOrderService;
import com.aizhixin.lab.project.course.vo.TeacherInfoVo;
import com.aizhixin.lab.project.template.domain.DeleteDomain;
import com.aizhixin.lab.project.template.domain.WorkOrderDomain;
import com.aizhixin.lab.project.template.service.WorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RequestMapping("/api/v1/teacher/project/workOrder")
@RestController
@Api(tags = "工单",value = "工单模块")
public class TeacherWorkOrderController {
    @Autowired
    private TeacherTaskWorkOrderService workOrderService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/updateSort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", httpMethod = "POST", notes = "修改排序<br><br><b>lwq</b>")
    public ResponseEntity<?> updateSort(@RequestHeader("Authorization") String token, @RequestBody List<String> ids) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        workOrderService.updateSort(ids);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "修改成功");
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存/编辑", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody WorkOrderDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            String save = workOrderService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findWorkOrderList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据任务ID获取该任务下工单列表", httpMethod = "GET", notes = "根据任务ID获取该任务下工单列表<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = WorkOrderDomain.class),})
    public ResponseEntity<?> findWorkOrderList(@RequestHeader("Authorization") String token, @RequestParam String moduleId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<WorkOrderDomain> workOrderByModuleId = workOrderService.findWorkOrderByModuleId(moduleId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,workOrderByModuleId);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findWorkOrderInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取工单详情", httpMethod = "GET", notes = "获取工单详情<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = WorkOrderDomain.class),})
    public ResponseEntity<?> findWorkOrderInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(id)){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.ERROR,"id不能为空");
            return new ResponseEntity(result, HttpStatus.OK);
        }

        result = workOrderService.findWorkOrder(id,account);


        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除工单", httpMethod = "POST", notes = "删除工单<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @RequestBody DeleteDomain deleteDomain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            result=workOrderService.delete(deleteDomain);

        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
}
